//
// TODO: move to a common js file
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) {
    endstr = document.cookie.length;
  }
  return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal (j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function setCookie (name, value) {
  var argv = setCookie.arguments;
  var argc = setCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
}

function deleteCookie () {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1000000000);  // This cookie is history (changed -1 to make it previous time)
  var cval = getCookie ('DemoName');
  document.cookie ='DemoName' + "=" + cval + "; expires=" + exp.toGMTString();
}

var TopicParser = {
  MARK_KEY_1 : "#key.1#",

  MARK_TEXT : "#text#",
  
  PATTERN_SITE_MAP : new Array(
    new Array("#\((\\d+)\)", "<a class=\"hotlink\" href=\"http://forum.htkou.net/index.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#(\\d+)@htkou", "<a class=\"hotlink\" href=\"http://forum.htkou.net/index.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#(\\d+)@rolia", "<a class=\"hotlink\" href=\"http://www.rolia.net/f/post.php?pno=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#(\\d+)@shuigang", "<a class=\"hotlink\" href=\"http://www.shuigang.org/forum/showpost.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#([\\w\\s]+)@google", "<a class=\"hotlink\" href=\"http://www.google.ca/search?q=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#([\\w\\s]+)@youtube", "<a class=\"hotlink\" href=\"http://www.youtube.com/watch?v=#key.1#\" target=\"_forgein\">#text#</a>")
  ),
  run : function() {
    this.checkThreads();
  },

  checkThreads : function() {
    var contentElement = document.getElementById("content");
    var threads = contentElement.getElementsByTagName("ul")
  
    for(var i=0; i<threads.length; i++) {
      if(threads[i].className.indexOf('thread') >= 0) {
        var thread = threads[i]; 
        var folded = false;
        // TODO: check the existing mark
        if(getCookie(this.getCookieName(thread.id)) != null) {
          folded = true;
        }
        if(!folded) {
          var link = this.createFoldLink(thread.id);
          thread.insertBefore(link, thread.firstChild);
        } else {
          var link = this.createExpandLink(thread.id);
          thread.insertBefore(link, thread.firstChild);
          this.foldThread(thread.id);
        }
      }
    }
  },

  createFoldLink : function(threadId) {
    var img = document.createElement('img');
    img.src = 'templates/default/images/fold_thread.png';
    var link = document.createElement('a');
    link.href = "javascript:TopicParser.foldThread('" + threadId + "')";
    link.appendChild(img);
    return link;
  },

  createExpandLink : function(threadId) {
    var img = document.createElement('img');
    img.src = 'templates/default/images/expand_thread.png';
    var link = document.createElement('a');
    link.href = "javascript:TopicParser.expandThread('" + threadId + "')";
    link.appendChild(img);
    return link;
  },

  foldThread : function(threadId) {
    var thread = document.getElementById(threadId);

    thread.replaceChild(this.createExpandLink(threadId), thread.firstChild);
    var li = thread.childNodes[1];
    for(var i=0; i<li.childNodes.length; i++) {
      if(li.childNodes[i].tagName == 'UL') {
        li.childNodes[i].style.display = 'none';
      }
    }
    
    var expdate = new Date (); 
    expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000)); 
    setCookie(this.getCookieName(threadId), 'f', expdate);
  },
  
  expandThread : function(threadId) {
    var thread = document.getElementById(threadId);

    thread.replaceChild(this.createFoldLink(threadId), thread.firstChild);
    var li = thread.childNodes[1];
    for(var i=0; i<li.childNodes.length; i++) {
      if(li.childNodes[i].tagName == 'UL') {
        li.childNodes[i].style.display = 'list-item';
      }
    }

    deleteCookie(this.getCookieName(threadId));
  },
  
  getCookieName : function(threadId) {
    return 'folded.' + threadId;
  }
}

TopicParser.run();
